package com.usian.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.user.utils.YongyouyunAuthUtils;
import com.usian.utils.common.exception.CatchCustomException;
import com.usian.utils.common.exception.code.UserStatusCode;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.user.mapper.ApUserRealnameMapper;
import com.usian.user.service.ApUserRealnameService;
//import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class ApUserRealnameServiceimpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService{
    @Autowired
    private  ApUserRealnameMapper apUserRealnameMapper;

    @Override
    public ResponseResult lpadListByStatus(AuthDto dto) {
        if (dto==null){
            CatchCustomException.catchs(UserStatusCode.PARAM_FAIL,"参数不合法");
        }
        dto.checkParam();
        //条件查询
        QueryWrapper<ApUserRealname> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(ApUserRealname::getStatus,dto.getStatus());
//        queryWrapper.lambda().eq(" ","");
        LambdaQueryWrapper<ApUserRealname> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        if (dto.getStatus()!=null){
            //用的是引用数据类型
            lambdaQueryWrapper.eq(ApUserRealname::getStatus,dto.getStatus());
        }
        Page<ApUserRealname> page=new Page<>(dto.getPage(),dto.getSize());
        IPage<ApUserRealname> page1 = this.page(page, lambdaQueryWrapper);
        ResponseResult responseResult=new PageResponseResult(dto.getPage(),dto.getSize(),(int)page1.getTotal());
        responseResult.setData(page1.getRecords());
        return responseResult;
    }



    /**
     * 实名认证
     * 缺失：1. 如何触发自动审核方法
     * @param dto
     * @return
     */
    @Override
//    @GlobalTransactional
    public ResponseResult AutoUpdateStatus(AuthDto dto) {
//        boolean flag = true;
//        if(dto ==null){
//            CatchCustomException.catchs(UserStatusCode.PARAM_FAIL, "参数不合法！");
//        }
//        // 1. 根据Id查询用户实名认证信息
//        ApUserRealname apUserRealname =  this.getById(dto.getId());
//        if(apUserRealname==null){
//            CatchCustomException.catchs(UserStatusCode.PARAM_FAIL, "认证用户信息有误！");
//        }
//        // 2. 状态为待审核状态进入审核流程  0 创建中  1 待审核  2 审核失败  8 自动审核失败，待人工审核  9 审核通过
//        if(apUserRealname.getStatus()!=1){
//            CatchCustomException.catchs(UserStatusCode.PARAM_FAIL, "当前数据状态有误，请检查之后重试！");
//        }
//        // 3. 3.1 先身份证 2.活体检测 3.人证合一
//        // 1. 先根据身份证正面照片进行身份证OCR识别
//        Map map = new HashMap();
//        map.put("image",apUserRealname.getFontImage()); //身份证正面照片
//        map.put("imageType","URL");
//        map.put("ocrType","0");
//        ResponseEntity<String> ocrEntity =  YongyouyunAuthUtils.conns("https://api.yonyoucloud.com/apis/dst/IdcardOCR/IdcardOCR","df2abd057fbe4157b2524d0398b81fd8 ",map);
//        Map OcrMap =  JSON.parseObject(ocrEntity.getBody(),Map.class);
//        Map data =  JSON.parseObject(OcrMap.get("data")+"",Map.class);
//        String cardNum = (String) data.get("cardNum");
//        String name = (String) data.get("name");
//        System.out.println(name);
//        System.out.println(cardNum);
//        if("成功".equals(OcrMap.get("message"))){ //成功
//            //调用 身份证二要素接口  // 判断success = true
//            Map map1 = new HashMap();
//            map1.put("idNumber",cardNum);
//            map1.put("userName",name);
//            ResponseEntity<String> matchIdentity =  YongyouyunAuthUtils.conns("https://api.yonyoucloud.com/apis/dst/matchIdentity/matchIdentity"," d318cc971edb419193f6bbf1736f7a6e ",map1);
//            Map MiMap =  JSON.parseObject(matchIdentity.getBody(),Map.class);
//            if ("一致".equals(MiMap.get("message"))){
//                //调用 活体检测接口  // 判断message = 成功
//                Map map2 = new HashMap();
//                map2.put("image",apUserRealname.getLiveImage());
//                map2.put("imageType","URL");
//                ResponseEntity<String> Biologicalexamination  =  YongyouyunAuthUtils.conns("https://api.yonyoucloud.com/apis/dst/Biologicalexamination/Biologicalexamination"," b6390ad1d92c42739e13e144fb3f7976",map2);
//                Map BglMap =  JSON.parseObject(Biologicalexamination.getBody(),Map.class);
//                if ("成功".equals(BglMap.get("message"))){
//                    //调用 人证核验接口 //判断message = 成功
//                    Map map3 = new HashMap();
//                    map3.put("idCardImage",apUserRealname.getFontImage());
//                    map3.put("imageType","URL");
//                    map3.put("liveImage",apUserRealname.getLiveImage());
//                    ResponseEntity<String> ComparisonsofPersonsandDocuments  =  YongyouyunAuthUtils.conns("https://api.yonyoucloud.com/apis/dst/ComparisonsofPersonsandDocuments/ComparisonsofPersonsandDocuments","2d2bed892d724ebeb62409027106c971",map3);
//                    Map CfpMap =  JSON.parseObject(ComparisonsofPersonsandDocuments.getBody(),Map.class);
//                }else {
//                    flag = false;
//                }
//            }else {
//                flag = false;
//            }
//        }else{
//            flag = false;
//        }
//
//        // 成功和失败  成功 -- 状态改正9
//        if(flag){
//            apUserRealname.setStatus((short)9);
//            this.updateById(apUserRealname);
//        }else{
//            // 自动审核失败 修改状态  == 8  // 失败： 直接修改状态  == 8
//            apUserRealname.setStatus((short)8);
//            this.updateById(apUserRealname);
//        }
        return null;
    }

    @Override
//    @GlobalTransactional
    public ResponseResult updateStatus(AuthDto dto, Short authorType) {
        if (dto == null ||dto.getId() == null){
            CatchCustomException.catchs(UserStatusCode.PARAM_FAIL,"参数不合法");
        }
        ApUserRealname apUserRealname= apUserRealnameMapper.selectById(dto.getId());
        if (dto.getMsg()!=null){
            apUserRealname.setStatus((short)9);
            apUserRealname.setReason(dto.getMsg());
            apUserRealnameMapper.updateById(apUserRealname);
        }
        apUserRealname.setStatus(authorType);
        apUserRealnameMapper.updateById(apUserRealname);
        return ResponseResult.okResult(UserStatusCode.SUCCESS,"成功");
    }
}
